$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/chargestandard/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '时间名称(小时)', name: 'timename', index: 'timename', width: 80 },
			{ label: '对应时间所需钱数(元)', name: 'timemoney', index: 'timemoney', width: 80 },
            { label: '操作', name: 'sy', width: 100,align:'center',formatter: function(cellvalue, options, rowdata) {
                    return "<a style='cursor:pointer;' class='btn btn-primary' onclick='vm.update("+ rowdata.id +")'>" +
                        "修改</a>";
                }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		chargestandard: [],
        standar:[],
        temp:{}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.chargestandard = {};

			vm.standar = [];
			vm.chargestandard = [];
			vm.temp = {};
            $("#btn").show();
            $("#sfbz").html("");
            var temp = "<div style='width:800px; '><div style='float:left;margin-top:7px;margin-right:3px;'>时间名称</div><div class='col-sm-10' style='width:300px;'>" +
                "<input type='text' class='form-control' name='tname' placeholder=''/>" +
                "<span style='position: absolute;right: 22px;top: 8px;color: #4C4948;line-height: 1.5;'>小时</span></div>" +
                "<div style='float:left;margin-top:7px;margin-right:3px;'>对应时间所需钱数</div><div class='col-sm-10' style='width:300px;float:left;;'>" +
            "<input type='text' class='form-control' name='tmoney' placeholder=''/>" +
                "<span style='position: absolute;right: 22px;top: 8px;color: #4C4948;line-height: 1.5;'>元</span></div></div>";
            $("#sfbz").append(temp);
		},
		update: function (id) {

			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id);
            $("#btn").hide();
            $("#sfbz").html("");
            var temp = "<div style='width:800px;  float:left;' ><div style='float:left;margin-top:7px;margin-right:3px;' >时间名称</div><div class='col-sm-10' style='width:300px;'>" +
                    "<input type='text' class='form-control' value='"+vm.temp.timename+"' name='tname' placeholder=''/>" +
                    "<span style='position: absolute;right: 22px;top: 8px;color: #4C4948;line-height: 1.5;'>小时</span></div>" +
                    "<div style='float:left;margin-top:7px;margin-right:3px;'>对应时间所需钱数</div><div class='col-sm-10' style='width:300px;float:left;;'>" +
                    "<input type='text' class='form-control' value='"+vm.temp.timemoney+"' name='tmoney' placeholder=''/>" +
                    "<span style='position: absolute;right: 22px;top: 8px;color: #4C4948;line-height: 1.5;'>元</span></div></div></div>";

            $("#sfbz").append(temp);

		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.temp.id == null ?"sys/chargestandard/save":"sys/chargestandard/update";
                var tname = [];
                var tmoney = [];

                vm.temp.data = "";
                $("input[name='tname']").each(function(){
                    if($(this).val() == ''){
                        alert("时间不能为空");
                        return;
                    }
                    tname.push($(this).val());
                });
                $("input[name='tmoney']").each(function(){
                    if($(this).val() ==''){
                        alert("钱数不能为空");
                        return;
                    }
                    tmoney.push($(this).val());
                });
                for(var i=0;i<tname.length;i++){
                    vm.temp.data +=tname[i]+"-"+tmoney[i];
                    if(i!=tname.length-1){
                        vm.temp.data +=",";
                    }
                }
                console.log(vm.temp)
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.temp),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
        addBusTime: function () {
            var temp = "";
            if(vm.standar.length>0){
                temp = "<div style='width:800px; float:left;margin-top:5px; ' id='sj"+(vm.standar.length+1)+"'><div style='float:left;margin-top:7px;margin-right:3px;' >时间名称</div><div class='col-sm-10' style='width:300px;'>" +
                    "<input type='text' class='form-control' v-model='chargestandard.timename' name='tname' placeholder=''/>" +
                    "<span style='position: absolute;right: 22px;top: 8px;color: #4C4948;line-height: 1.5;'>小时</span></div>" +
                    "<div style='float:left;margin-top:7px;margin-right:3px;'>对应时间所需钱数</div><div class='col-sm-10' style='width:300px;float:left;;'>" +
                    "<input type='text' class='form-control' v-model='chargestandard.timemoney' name='tmoney' placeholder=''/>" +
                    "<span style='position: absolute;right: 22px;top: 8px;color: #4C4948;line-height: 1.5;'>元</span></div><div style='float: left;padding-left: 3px;padding-top:8px;'><a>" +
                    "<img style='cursor: pointer' src='../../statics/img/ico16-delete.png' width='16' height='16' onclick=vm.removeBusTime('sj"+(vm.standar.length+1)+"')></a></div></div>";


            }else{
                temp = "<div style='width:800px; float:left;margin-top:5px;' id='sj1'><div style='float:left;margin-top:7px;margin-right:3px;' >时间名称</div><div class='col-sm-10' style='width:300px;'>" +
                    "<input type='text' class='form-control' v-model='chargestandard.timename' name='tname' placeholder=''/>" +
                    "<span style='position: absolute;right: 22px;top: 8px;color: #4C4948;line-height: 1.5;'>小时</span></div>" +
                    "<div style='float:left;margin-top:7px;margin-right:3px;'>对应时间所需钱数</div><div class='col-sm-10' style='width:300px;float:left;;'>" +
                    "<input type='text' class='form-control' v-model='chargestandard.timemoney' name='tmoney' placeholder=''/>" +
                    "<span style='position: absolute;right: 22px;top: 8px;color: #4C4948;line-height: 1.5;'>元</span></div><div style='float: left;padding-left: 3px;padding-top:8px;'><a>" +
                    "<img style='cursor: pointer' src='../../statics/img/ico16-delete.png' width='16' height='16' onclick=vm.removeBusTime('sj1')></a></div></div>";

                var obj = {};
                obj.id = 1;
                vm.standar.push(obj);
            }

            $("#sfbz").append(temp);
        },
        removeBusTime: function (id) {
            $("#"+id).remove();
        },
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/chargestandard/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/chargestandard/info/"+id,
                success: function (r) {
                    vm.temp = r.chargestandard;
                }
            });

		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});